package com.zzyl.nursing.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzyl.common.exception.base.BaseException;
import com.zzyl.common.utils.UserThreadLocal;
import com.zzyl.nursing.domain.Elder;
import com.zzyl.nursing.domain.FamilyMemberElder;
import com.zzyl.nursing.dto.MemberElderDto;
import com.zzyl.nursing.mapper.ElderMapper;
import com.zzyl.nursing.mapper.FamilyMemberElderMapper;
import com.zzyl.nursing.service.IElderService;
import com.zzyl.nursing.service.IFamilyMemberElderService;
import com.zzyl.nursing.vo.FamilyMemberElderVo;
import com.zzyl.nursing.vo.MemberElderVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 客户老人关联Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-05-19
 */
@Service
public class FamilyMemberElderServiceImpl extends ServiceImpl<FamilyMemberElderMapper, FamilyMemberElder> implements IFamilyMemberElderService
{
    @Autowired
    private FamilyMemberElderMapper familyMemberElderMapper;

    @Autowired
    private ElderMapper elderMapper;
    @Autowired
    private IElderService elderService;

//    /**
//     * 查询客户老人关联
//     *
//     * @param id 客户老人关联主键
//     * @return 客户老人关联
//     */
//    @Override
//    public FamilyMemberElder selectFamilyMemberElderById(Long id)
//    {
//        return getById(id);
//    }
//
//    /**
//     * 查询客户老人关联列表
//     *
//     * @param familyMemberElder 客户老人关联
//     * @return 客户老人关联
//     */
//    @Override
//    public List<FamilyMemberElder> selectFamilyMemberElderList(FamilyMemberElder familyMemberElder)
//    {
//        return familyMemberElderMapper.selectFamilyMemberElderList(familyMemberElder);
//    }
//
//    /**
//     * 新增客户老人关联
//     *
//     * @param familyMemberElder 客户老人关联
//     * @return 结果
//     */
////    @Override
////    public int insertFamilyMemberElder(FamilyMemberElder familyMemberElder)
////    {
////        return save(familyMemberElder) ? 1 : 0;
////    }
//
//    /**
//     * 修改客户老人关联
//     *
//     * @param familyMemberElder 客户老人关联
//     * @return 结果
//     */
//    @Override
//    public int updateFamilyMemberElder(FamilyMemberElder familyMemberElder)
//    {
//        return updateById(familyMemberElder) ? 1 : 0;
//    }
//
//    /**
//     * 批量删除客户老人关联
//     *
//     * @param ids 需要删除的客户老人关联主键
//     * @return 结果
//     */
//    @Override
//    public int deleteFamilyMemberElderByIds(Long[] ids)
//    {
//        return removeByIds(Arrays.asList(ids)) ? 1 : 0;
//    }
//
//    /**
//     * 删除客户老人关联信息
//     *
//     * @param id 客户老人关联主键
//     * @return 结果
//     */
//    @Override
//    public int deleteFamilyMemberElderById(Long id)
//    {
//        return removeById(id) ? 1 : 0;
//    }

    @Override
    public int add(MemberElderDto memberElderDto) {
        //1.判断该老人是否存在
        LambdaQueryWrapper<Elder> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Elder::getIdCardNo, memberElderDto.getIdCard());
        Elder elder = elderMapper.selectOne(lambdaQueryWrapper);
        if (ObjectUtil.isEmpty(elder)) {
            throw new BaseException("该老人不存在");
        }

        //2.判断该老人是否已经绑定
        //2.1 获取当前登录用户
        Long userId = UserThreadLocal.getUserId();
        //2.2 判断该老人是否已经绑定
        long count = count(Wrappers.<FamilyMemberElder>lambdaQuery()
                .eq(FamilyMemberElder::getElderId, elder.getId())
                .eq(FamilyMemberElder::getFamilyMemberId, userId));
        if (count > 0) {
            throw new BaseException("该老人已经绑定");
        }

        //3.保存
        FamilyMemberElder familyMemberElder = BeanUtil.toBean(memberElderDto, FamilyMemberElder.class);
        familyMemberElder.setFamilyMemberId(userId);
        familyMemberElder.setElderId(elder.getId());

        return save(familyMemberElder)?  1 : 0;
    }

    @Override
    public List<FamilyMemberElderVo> my() {
        Long userId = UserThreadLocal.getUserId();
        List<FamilyMemberElderVo> list = familyMemberElderMapper.selectByMemberId(userId);
        return list;
    }

    @Override
    public List<MemberElderVo> listByPage(Long userId) {
        return familyMemberElderMapper.listByPage(userId);
//        List<FamilyMemberElder> list = this.list(new LambdaQueryWrapper<FamilyMemberElder>().eq(FamilyMemberElder::getFamilyMemberId, userId));
//        List<MemberElderVo> memberList = list.stream().map(item -> {
//            MemberElderVo memberElderVo = new MemberElderVo();
//            Long elderId = item.getElderId();
//            Elder one = elderService.getOne(new LambdaQueryWrapper<Elder>().eq(Elder::getId, elderId));
//            BeanUtil.copyProperties(item, memberElderVo);
//            BeanUtil.copyProperties(one, memberElderVo);
//            return memberElderVo;
//        }).collect(Collectors.toList());
//        return memberList;
    }

    @Override
    public int deleteById(Long id) {
        return removeById(id) ? 1 : 0;
    }
}
